﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Web;
using System.Web.Caching;

namespace UtilsLib.UtilsCaching
{
    public static class WebCacheManager
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        /// <param name="timeout"></param>
        /// <param name="callback"></param>
        public static void Set(string key, object obj, TimeSpan timeout)
        {
            Set(key, obj, timeout, null, null);
        }
        public static void Set(string key, object obj, DateTime datetime)
        {
            Set(key, obj, datetime, null, null);
        }
        public static void Set(string key, object obj, TimeSpan timeout, CacheItemRemovedCallback callback)
        {
            Set(key, obj, timeout, callback, null);
        }
        public static void Set(string key, object obj, DateTime datetime, CacheItemRemovedCallback callback)
        {
            Set(key, obj, datetime, callback, null);
        }

        public static void Set(string key, object obj, TimeSpan timeout, CacheItemRemovedCallback callback, CacheDependency dependency)
        {
            HttpRuntime.Cache.Add(
              key,
              obj,
              dependency,
              Cache.NoAbsoluteExpiration,
              timeout,
              CacheItemPriority.Default,
              callback);

        }

        public static void Set(string key, object obj, DateTime datetime, CacheItemRemovedCallback callback, CacheDependency dependency)
        {
            HttpRuntime.Cache.Add(
              key,
              obj,
              dependency,
              datetime,
              Cache.NoSlidingExpiration,
              CacheItemPriority.Default,
              callback);
        }

        public static object Get(string key)
        {
            return HttpRuntime.Cache[key] ?? null;
        }

        public static void Remove(string key)
        {
            if (Get(key) != null)
                HttpRuntime.Cache.Remove(key);
        }

        /// <summary>
        /// Calculates the lenght in bytes of an object 
        /// and returns the size 
        /// </summary>
        /// <param name="o">Object to test</param>
        /// <returns></returns>
        public static int GetObjectSize(object o)
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            byte[] Array;
            bf.Serialize(ms, o);
            Array = ms.ToArray();
            return Array.Length;
        }

        public static string GetApplicationCacheSize()
        {
            Int64 count = GetObjectSize(HttpRuntime.Cache);
            return "Cache size : " + count.ToString() + " b ";
        }
    }
}
